package com.bsg.upm.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsg.upm.cache.DictTypeCache;
import com.bsg.upm.constant.OperateDictTypeConstants;
import com.bsg.upm.dao.OperateLogDao;
import com.bsg.upm.entity.DictEntity;
import com.bsg.upm.entity.OperateLogEntity;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.util.DateUtils;

@Service
public class OperateLogService extends BaseService {
	@Resource
	private OperateLogDao operateLogDao;
	@Resource
	private DictTypeCache dictTypeCache;
	 

	public RespJson list(Map<String, Object> paramMap) throws APIException {
		JSONArray ret = new JSONArray();
		List<OperateLogEntity> logs = operateLogDao.list(paramMap);
		if (logs.size() == 0) {
			return RespJsonFactory.buildOK(ret);
		}

		for (OperateLogEntity log : logs) {
			ret.add(getShowObjFromEntity(log));
		}
		
		return RespJsonFactory.buildOK(ret);
	}

	private JSONObject getShowObjFromEntity(OperateLogEntity operateLogEntity) {
		JSONObject object = new JSONObject();
		String actionName = null;
		String modelName = null;
		
		object.put("user", operateLogEntity.getCreateUserLoginName());
		object.put("create_time", DateUtils.dateTimeToString(operateLogEntity.getCreateDateTime()));
		
		DictEntity actionEntity = dictTypeCache.getDictFromCache(OperateDictTypeConstants.OPERATE_ACTION, operateLogEntity.getAction());
		if (actionEntity !=null){
			 actionName = actionEntity.getName();
		}
	 
		DictEntity modelEntity = dictTypeCache.getDictFromCache(OperateDictTypeConstants.OPERATE_MODEL, operateLogEntity.getModelName());
		if (modelEntity !=null){
			modelName=modelEntity.getName();
		}
	
		object.put("action", actionName);
		object.put("model", modelName);
		
		return object;
	}
}
